import asyncio
import websockets
import csv
import time

async def simulate_ws_sender(ws_url, csv_file, interval=0.5):
    """
    Simulate sending WebSocket messages from a CSV file.
    
    :param ws_url: WebSocket server URL.
    :param csv_file: Path to the CSV file containing data.
    :param interval: Time interval (in seconds) between each message.
    """
    async with websockets.connect(ws_url) as websocket:
        with open(csv_file, 'r') as file:
            reader = csv.DictReader(file)
            for row in reader:
                # Create the message in "utm|Easting|Northing|Yaw" format
                message = f"gps|{row['Easting']}|{row['Northing']}|{row['Yaw']}"
                print(f"Sending: {message}")
                await websocket.send(message)  # Send the message
                await asyncio.sleep(interval)  # Wait for the specified interval

def start_simulation(ws_url, csv_file, interval=0.5):
    """
    Start the simulation to send WebSocket messages.

    :param ws_url: WebSocket server URL.
    :param csv_file: Path to the CSV file containing data.
    :param interval: Time interval (in seconds) between each message.
    """
    loop = asyncio.get_event_loop()
    loop.run_until_complete(simulate_ws_sender(ws_url, csv_file, interval))


if __name__ == "__main__":
    # WebSocket server URL
    ws_url = "ws://39.106.77.250:8089/ws?command_sender&gps"  # Replace with your WebSocket server URL
    
    # Path to the CSV file
    csv_file = r"C:\Users\17963\Desktop\ros_workspace\navigation2_tutorials\my_test_dir\gps\utm_data.csv"  # Update with the correct file path
    
    # Time interval between messages
    interval = 0.05  # Adjust as needed

    # Start the simulation
    start_simulation(ws_url, csv_file, interval)
